/**
 * Copyright (C) 2013 Inera AB (http://www.inera.se)
 *
 * This file is part of Inera Certificate Proxy (http://code.google.com/p/inera-certificate-proxy).
 *
 * Inera Certificate Proxy is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Inera Certificate Proxy is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package se.inera.certificate.proxy.mappings.local;

import org.apache.commons.lang.builder.ToStringBuilder;
import se.inera.certificate.proxy.filter.RequestContext;
import se.inera.certificate.proxy.mappings.Dispatcher;
import se.inera.certificate.proxy.mappings.Mapping;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.apache.commons.lang.StringUtils.substringAfter;
import static org.apache.commons.lang.builder.ToStringStyle.SIMPLE_STYLE;

public class LocalMapping implements Mapping {

    /** The context to map from e.g. /m/module1 **/
    private String context;

    /** The path to map to e.g. /module1servlet **/
    private String mappedPath;

    public LocalMapping(String context, String mapTo) {
        checkNotNull(context, "context is null");
        checkNotNull(mapTo, "mapTo is null");
        checkArgument(mapTo.startsWith("LOCAL:"), "mapTo must start with 'LOCAL:'");

        this.context = context;
        this.mappedPath = substringAfter(mapTo, "LOCAL:");
    }

    @Override
    public Dispatcher getDispatcher(RequestContext requestContext) {
        return new LocalDispatcher(this, requestContext);
    }

    @Override
    public String getContext() {
        return this.context;
    }

    @Override
    public String getMappedPath() {
        return this.mappedPath;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, SIMPLE_STYLE);
    }
}
